package com.yunshang.ys.base;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

//import com.risenbsy.ixing.R;
//import com.risenbsy.ixing.utils.ComplexUtil;
//import com.risenbsy.risenbsylib.base.RisActivity;
import com.yunshang.ys.R;
import com.yunshang.ys.utils.ComplexUtil;
import com.yunshang.ys.utils.zzUtils;
import com.yunshang.ys.ys.YSActivity;

import uk.co.chrisjenx.calligraphy.CalligraphyConfig;
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;


/**
 * Created by FengYi on 2016/10/9.
 */
public class BaseActivity extends YSActivity {
    /**
     * um
     */

   /* public SHARE_MEDIA platform = null;
    public UMAuthListener umAuthListener = null;*/
    private boolean isTitleShow = false;
    TextView statusbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//        setBackground(getResources().getColor(R.color.colorMain));
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);//默认不弹软键盘




        /**
         * init fonts
         */
        CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                .setDefaultFontPath("fonts/fzlthei.ttf")
                .setFontAttrId(R.attr.fontPath)
                .build());
        // init fonts




    }

    // 状态栏
    public void initstatusbar() {
        statusbar = (TextView) findViewById(R.id.vStatusBar);
        statusbar.setMinimumHeight(zzUtils.getStatusBarHeight(this));
        statusbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
    }


//    public BaseActivity isTransparent() {
//        if (!isTitleShow) return this;
////        View view = findViewById(R.id.vStatusBar);
////        View title = findViewById(R.id.titleBg);
////        View titleBar = findViewById(R.id.titleBar);
////        ComplexUtil.setTitleHeight(title, titleBar, view);
//        return this;
//    }

    @Override
    public BaseActivity showTitle(String titleName) {
        if (findViewById(R.id.titleBar) == null) return this;
        findViewById(R.id.titleBar).setVisibility(View.VISIBLE);
        TextView title = (TextView) findViewById(R.id.title);
        title.setVisibility(View.VISIBLE);
        title.setText(titleName);
        isTitleShow = true;
        return this;
    }

    @Override
    public BaseActivity withBack() {
        if (!isTitleShow) return this;
        RelativeLayout back = (RelativeLayout) findViewById(R.id.back);
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//                if (imm != null) {
//                    //[java] view plain copy 在CODE上查看代码片派生到我的代码片
//                    imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(),
//                            0);
//                }
                finish();
            }
        });
        return this;
    }

    @Override
    public BaseActivity withRightText(String text, View.OnClickListener onClickListener) {
        if (!isTitleShow) return this;
        TextView rightText = (TextView) findViewById(R.id.rightText);
        rightText.setVisibility(View.VISIBLE);
        rightText.setText(text);
        rightText.setOnClickListener(onClickListener);
        return this;
    }

    @Override
    public BaseActivity withRightIcon(int iconResourceId, View.OnClickListener onClickListener) {
        if (!isTitleShow) return this;
        ImageView rightBtn1 = (ImageView) findViewById(R.id.rightIcon1);
        rightBtn1.setVisibility(View.VISIBLE);
        rightBtn1.setImageResource(iconResourceId);
        rightBtn1.setOnClickListener(onClickListener);
        return this;
    }

    //处理标题栏背景等相关
    public BaseActivity setBackground(int titleResID) {
        if (!isTitleShow) return this;
        findViewById(R.id.titleBar).setBackgroundResource(titleResID);
        return this;
    }

    //处理标题栏背景颜色
    public BaseActivity setBackgroundColor(int color) {
        if (!isTitleShow) return this;
        findViewById(R.id.titleBar).setBackgroundColor(color);
        return this;
    }

    // 显示左边按钮图片
    public BaseActivity setLiftIamge(int image) {
        if (!isTitleShow) return this;
        ImageView imageView = (ImageView) findViewById(R.id.leftImage);
        imageView.setImageResource(image);
        return this;
    }

    //设置左边文字
    public BaseActivity setLiftText(String text) {
        if (!isTitleShow) return this;
        TextView textView = (TextView) findViewById(R.id.leftText);
        textView.setText(text);
        return this;
    }

    //注册监听左侧文字
    public BaseActivity setLiftTextOnClick(View.OnClickListener onClickListener) {
        if (!isTitleShow) return this;
        findViewById(R.id.leftBt).setOnClickListener(onClickListener);
        return this;
    }

    //设置title 背景
    public BaseActivity setBg(int bgId) {
        if (!isTitleShow) return this;
        findViewById(R.id.titleBg).setBackgroundResource(bgId);
        return this;
    }


//    @Override
//    public void showProgressDialog(String message, boolean cancelAble) {
//        try {
//            SingleProgressDialog.getInstance(getApplicationContext(), false).show();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    @Override
//    public void dismissProgressDialog() {
//        try {
//            SingleProgressDialog.getInstance(getApplicationContext(), false).dismiss();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    @Override
//    public void onDestroy() {
//        super.onDestroy();
//        SingleProgressDialog.makenull();
//    }

    /**
     * init fonts
     */
    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }
    // init fonts
}
